package com.sskj.bibi.presenter;

import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.bibi.bean.EntrustBean;
import com.sskj.bibi.ui.fragment.CurrentEntrustFragment;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.http.CallBackOption;
import com.sskj.lib.http.JsonConverter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;


public class CurrentEntrustFragmentPresenter extends BasePresenter<CurrentEntrustFragment> {


//    public void getEntrustListRefresh(String code, UserData userData) {
//        if (userData == null) {
//            mView.updateUI(new ArrayList<>());
//            return;
//        }
//        httpService.getRecordAll("1", null, code, "1", null)
//                .execute(new CallBackOption<HttpData<PageBean<EntrustBean>>>() {
//                }.unLoadBind(mView)
//                        .execute(httpData -> {
//                            if (httpData.getData() == null) {
//                                mView.updateUI(new ArrayList<>());
//                            } else {
//                                mView.updateUI(httpData.getData().getList());
//                            }
//                        }));
//    }

    public Flowable<List<EntrustBean>> getEntrustListRefresh(String code, UserData userData) {
        if (userData == null) {

            return Flowable.fromIterable(new ArrayList<EntrustBean>())
                    .toList()
                    .toFlowable();
        } else {
            return httpService.getRecordAll("1", null, code, "1", null)
                    .converter(new JsonConverter<HttpData<PageBean<EntrustBean>>>() {
                    })
                    .adapt(new FlowableBody<>())
                    .map(pageBeanHttpData -> pageBeanHttpData.getData().getList())
                    .onErrorReturnItem(new ArrayList<>());
        }

    }

    public void cancelEntrust(String orderId, String billType) {
        httpService.cancelCoinOrder(orderId, billType)
                .execute(new CallBackOption<HttpData>() {
                }.loadBind(mView)
                        .execute(httpData -> {
                            ToastUtil.showShort(httpData.getMsg());
                            mView.cancelReturn();
                        }));
    }


}
